package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.pojo.BookOrder;
import com.bdqn.service.BookOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 营业统计控制器
 * @author Administrator
 *
 */
@RequestMapping("/admin/stats")
@Controller
public class StatsController {

	@Resource
    BookOrderService bookOrderService;
	
	/**
	 * 统计页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/stats",method= RequestMethod.GET)
	public ModelAndView stats(ModelAndView model){
		model.setViewName("stats/stats");
		return model;
	}


	@ResponseBody // 直接将数据响应（通常使用Ajax调用时要加该注解）
	@RequestMapping("/get_stats")
	public Map get_stats(Model model, String type) {
		System.out.println("888888"+type);
		Map map = new HashMap();


		if (StringUtils.isEmpty(type)) {
			map.put("msg", "请选择统计类型!");
			return map;
		}

		switch (type) {
			case "day": {
				List<BookOrder>list1=bookOrderService.bookOrder();
				String string = JSON.toJSONString(list1);
				map.put("data",string);
				return map;
			}
			case "month": {
				List<BookOrder>list2=bookOrderService.year();
				String string1 = JSON.toJSONString(list2);
				map.put("data",string1);
				return map;
			}
			default: {
				map.put("type", "error");
				map.put("msg", "请选择正确的统计类型!");
				return map;
			}
		}
	}
}
